package www.nfin.ya.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import www.nfin.ya.entity.SqlTemplate;
import www.nfin.ya.service.IReaderSql;
import www.nfin.ya.service.SystemContextServer;
import www.nfin.ya.utils.SuperOracleUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @Projectname: uml-store20220926
 * @Filename: w
 * @Author: ��ʤΰ
 * @Data:2022/9/26 20:06
 * @Description: ͨ��excel��ȡsqlģ��
 */
public class ReaderSqlExcel implements IReaderSql {

    private static File file = null;
    static {
        Properties config = SystemContextServer.configProperties;
        file = new File((String) config.get("excelSourceFile"));
        if(!file.exists()){
            throw new RuntimeException(config.get("excelSourceFile")+"����һ����Ч�ļ�");
        }
    }
    @Override
    public List<SqlTemplate> getSql(int nodeId,int parentNodeId) {

        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(0);
        importParams.setHeadRows(1);
        List<SqlTemplate> sqlTemplates = ExcelImportUtil.importExcel(file, SqlTemplate.class, importParams);


        List<SqlTemplate> result = new ArrayList<>();
        for (int i = 0; i < sqlTemplates.size(); i++) {
            if(     (nodeId < 0 || sqlTemplates.get(i).getNodeId() == nodeId) &&
                    (parentNodeId < 0 || sqlTemplates.get(i).getParentNode() == parentNodeId)
                    && sqlTemplates.get(i).getBody() != null){
                SqlTemplate temp = sqlTemplates.get(i);
                temp.setBody(SuperOracleUtils.instead(temp.getBody()));
                result.add(temp);
            }
        }
        return result;
    }



}



